home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / amiga / v3_1 / sbp3_1e.lzh / READSTR.PL < prev    next >
Text File  |  1991-10-31  |  3KB  |  59 lines

  1. /* From the book PROLOG PROGRAMMING IN DEPTH
  2.    by Michael A. Covington, Donald Nute, and Andre Vellino.
  3.    Copyright 1988 Scott, Foresman & Co.
  4.    Non-commercial distribution of this file is permitted. */
  5.  
  6. /* READSTR.PL */
  7. /* Contains utility procedures READSTRING, WRITESTRING, and READATOM */
  8. /* See text for further documentation. */
  9.  
  10. /*************************************************************************
  11.  * readstring(Result)                                                    *
  12.  *   Displays a one-character-prompt, then accepts a line of input from  *
  13.  *   the keyboard and returns it as a string (list of ASCII codes).      *
  14.  *   The user can use Backspace (Ctrl-H) to make corrections, and is     *
  15.  *   prevented from backspacing past the beginning of the line.          *
  16.  *************************************************************************/
  17.  
  18. readstring(Result) :- put(62),       /* Prompt character is '>' */
  19.                       readstring_start(0,Result).
  20.  
  21. readstring_start(Count,X) :- get0(Char),
  22.                              readstring_aux(Count,Char,X).
  23.  
  24. readstring_aux(_,13,[]) :- !.        /* Return */
  25. readstring_aux(_,10,[]) :- !.        /* New line */
  26.  
  27. readstring_aux(0,8,Result) :-  !,    /* Backspace past beginning */
  28.                                readstring(Result).     /* start over */
  29.  
  30. readstring_aux(Count,8,Result) :- !, /* Backspace */
  31.                             put(32), /* Print a blank, then */
  32.                             put(8),  /* backspace again */
  33.                             NewCount is Count-1,
  34.                             readstring_start(NewCount,Tail),
  35.                             readstring_cons(8,Tail,Result).
  36.  
  37. readstring_aux(Count,Char,Result) :-    /* Normal character */
  38.                             NewCount is Count+1,
  39.                             readstring_start(NewCount,Tail),
  40.                             readstring_cons(Char,Tail,Result).
  41.  
  42. readstring_cons(Char,[8|Tail],Tail) :- Char \== 8, !.
  43. readstring_cons(Char,Tail,[Char|Tail]).
  44.  
  45. /*************************************************************************
  46.  * writestring(String)                                                   *
  47.  *  Displays a string (a list of ASCII codes).                           *
  48.  *************************************************************************/
  49.  
  50. writestring([]).
  51. writestring([Head|Tail]) :- put(Head), writestring(Tail).
  52.  
  53. /*************************************************************************
  54.  * readatom(Atom)                                                        *
  55.  *   Reads a string and then converts it to an atom.                     *
  56.  *************************************************************************/
  57.  
  58. readatom(Atom) :- readstring(String), name(Atom,String).
  59.